home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.06 Oct 92 / One-Application Patches / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  1.6 KB  |  83 lines  |  [TEXT/KAHL]

  1. /*    ------------------------------------------
  2.         events.c
  3.         
  4.         THINK C "Set Project Type..." settings:
  5.         code resource, type 'OAPn', ID 1000,
  6.         custom header, preloaded and locked,
  7.         file type 'rsrc', file creator 'RSED'.
  8.         ------------------------------------------
  9. */
  10. #include <Traps.h>
  11. #include "defs.h"
  12.  
  13. void main(void);
  14. void My_SystemEvent( void );
  15.  
  16. /* -------- global variables ---------- */
  17. long    Old_SystemEvent = NIL;
  18.  
  19. void main(void)
  20. {
  21.     long            save_A4;
  22.     
  23.     asm {
  24.         move.L        A4, save_A4
  25.         LEA                main, A4
  26.     }
  27.  
  28.     if (Old_SystemEvent == NIL)
  29.     {
  30.         Old_SystemEvent = GetToolTrapAddress(
  31.                     _SystemEvent );
  32.         SetToolTrapAddress( (long)My_SystemEvent,
  33.                     _SystemEvent );
  34.     }
  35.     
  36.     asm {
  37.         move.L        save_A4, A4
  38.     }
  39. }
  40.  
  41. /*    ------------------------------------------
  42.         My_SystemEvent        This head patch watches
  43.                                             events.
  44.         ------------------------------------------
  45. */
  46. void My_SystemEvent( void )
  47. {
  48.     EventRecord    *evt;
  49.     WindowPeek        front;
  50.     short                    res_index;
  51.     Handle                code_h;
  52.     OAPe_proc        Event_filter;
  53.     
  54.     asm {
  55.         movem.L        a0-a5/d0-d7, -(SP)    ; save registers
  56.         LEA                main, A4                    ; access to globals
  57.         move.L        8(A6), evt                ; copy event pointer
  58.     }
  59.     
  60.     front = (WindowPeek) FrontWindow();
  61.     if ( (front != NIL) &&
  62.         (front->windowKind != 2) && front->visible &&
  63.         front->hilited && front->goAwayFlag )
  64.     {
  65.         for (res_index = 1; ; ++res_index)
  66.         {
  67.             code_h = GetIndResource( 'OAPe', res_index );
  68.             if (code_h == NIL)
  69.                 break;
  70.             Event_filter = (OAPe_proc)
  71.                                                     StripAddress(*code_h);
  72.             Event_filter( evt );
  73.         }
  74.     }
  75.     
  76.     asm {
  77.         move.L        (A6), -4(A6)
  78.         move.L        Old_SystemEvent, (A6)
  79.         subQ            #4, A6
  80.         movem.L        (SP)+, A0-A5/D0-D7
  81.     }
  82. }
  83.